using System;
using System.ServiceModel;
using TimeLineControlNamedPipes.Communication;

namespace TimeLineControlNamedPipes
{
    public class ReceiverPipe : IDisposable
    {
        private readonly string _pipeName = string.Empty;
        private PipeService _ps = new PipeService();
        private bool _operational;
        private ServiceHost _host;

        public ReceiverPipe(string pipeName)
        {
            _pipeName = pipeName;
        }

        #region Public Operations
        // If the service host can be started,
        // true will be returned. If false is returned
        // another process owns the pipe.

        public bool ServiceOn(string baseAddress)
        {
            Error = null;
            return (_operational = HostThisService(baseAddress));
        }

        public void ServiceOff()
        {
            if (_host != null)
                if (_host.State != CommunicationState.Closed)
                    _host.Close();


            _operational = false;

        }

        #endregion

        #region Private WCF Operation

        // The thread which will listen for communications
        // from the other side of the pipe.
        private bool HostThisService(string baseAddress)
        {

            try
            {
                _host = new ServiceHost(_ps, new Uri(baseAddress));

                _host.AddServiceEndpoint(typeof(IPipeService),
                                          new NetNamedPipeBinding(),
                                          _pipeName);
                _host.Open();

                _operational = true;
            }
            catch (Exception ex)
            {
                Error = ex;
                _operational = false;
            }

            return _operational;

        }

        #endregion

        #region Public Properties

        // The consumer of this class will subscribe to this delegate to
        // receive data in a callback fashion.
        public PipeService.DataCurrentIsReady SlideChange
        {
            get
            {
                return _ps.SlideChangeReady;
            }

            set
            {
                _ps.SlideChangeReady += value;
            }
        }

        // The consumer of this class will subscribe to this delegate to
        // receive data in a callback fashion.
        public PipeService.DataIsReady SlidesDeckReady
        {
            get
            {
                return _ps.DataSlidesReady;
            }

            set
            {
                _ps.DataSlidesReady += value;
            }
        }

        // The consumer of this class will subscribe to this delegate to
        // receive data in a callback fashion.
        public PipeService.DataNameAppIsReady PowerPointChangeName
        {
            get
            {
                return _ps.DataAppReady;
            }

            set
            {
                _ps.DataAppReady += value;
            }
        }


        // The consumer of this class will subscribe to this delegate to
        // receive data in a callback fashion.
        public PipeService.DataPipeIsReady PipeIsConnected
        {
            get
            {
                return _ps.DataPipeReady;
            }

            set
            {
                _ps.DataPipeReady += value;
            }
        }

        // The consumer of this class will subscribe to this delegate to
        // receive data in a callback fashion.
        public PipeService.DataPipeShowBarReady TimeLineShowbar
        {
            get
            {
                return _ps.DataPipeShowBar;
            }

            set
            {
                _ps.DataPipeShowBar += value;
            }
        }

        // The consumer of this class will subscribe to this delegate to
        // receive data in a callback fashion.
        public PipeService.DataPipeSlideRunning SlideShowRunning
        {
            get
            {
                return _ps.DataSlideRunning;
            }

            set
            {
                _ps.DataSlideRunning += value;
            }
        }
        
        public string CurrentPipeName
        {
            get { return _pipeName; }
        }

        public Exception Error { get; private set; }

        #endregion

        #region IDisposable Members

        // A basic dispose.
        public void Dispose()
        {
            ServiceOff();
            _ps = null;
        }

        #endregion
    }
}
